package com.xyoto.community.task;

import com.xyoto.community.cache.CacheInstruction;
import com.xyoto.community.config.DynamicHotOperateAddScoreConfig;
import com.xyoto.community.constant.DeleteStatus;
import com.xyoto.community.constant.DynamicOperateType;
import com.xyoto.community.constant.LoveStatus;
import com.xyoto.community.mapper.comment.DynamicCommentMapper;
import com.xyoto.community.mapper.love.DynamicLoveMapper;
import com.xyoto.community.mapper.main.DynamicForwardMapper;
import com.xyoto.community.po.DynamicCommentCountPO;
import com.xyoto.community.po.DynamicForwardCountPO;
import com.xyoto.community.po.DynamicLoveCountPO;
import com.xyoto.community.utils.RedisCommonUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Random;

/**
 * Author: 廖振辉
 * Date: 2021/12/4
 * Description: 用于重新加载动态的点赞、评论、转发数到redis，并完成热门动态的添加，用于数据恢复
 */

//@SpringBootTest(classes = {CommunityApplication.class})
public class HotDynamicRecover {

    @Autowired
    private DynamicHotOperateAddScoreConfig dynamicHotOperateAddScoreConfig;

    @Autowired
    private DynamicLoveMapper dynamicLoveMapper;

    @Autowired
    private RedisCommonUtils redisCommonUtils;

    private Random random = new Random();

    @Autowired
    private DynamicCommentMapper dynamicCommentMapper;

    @Autowired
    private DynamicForwardMapper dynamicForwardMapper;

//    @Test
    public void refreshAllStatisticInCache(){
        // 清除redis中的热门动态
        redisCommonUtils.del(CacheInstruction.HOT_DYNAMIC_ID_SET);

        // 清除redis中的动态点赞数、转发数、评论数
        redisCommonUtils.del(CacheInstruction.DYNAMIC_LOVE_COUNT);
        redisCommonUtils.del(CacheInstruction.DYNAMIC_FORWARD_COUNT);
        redisCommonUtils.del(CacheInstruction.DYNAMIC_COMMENT_COUNT);

        initialHotDynamicCommentScore();
        initialHotDynamicLoveScore();
        initialHotDynamicForwardScore();
    }

    // 给动态加上对应的点赞分数，并加入到热门动态中
//    @Test
    public void initialHotDynamicLoveScore(){
        Double score = dynamicHotOperateAddScoreConfig.getDynamicIncreaseScoreByOperateType(DynamicOperateType.DYNAMIC_LOVE);
        List<DynamicLoveCountPO> dynamicLoveCountPOList = dynamicLoveMapper.countAllDynamic(LoveStatus.LOVE);
        for (DynamicLoveCountPO dynamicLoveCountPO : dynamicLoveCountPOList) {
            // 初始化，动态点赞数
            redisCommonUtils.hset(CacheInstruction.DYNAMIC_LOVE_COUNT
                    ,dynamicLoveCountPO.getDynamicId().toString(),dynamicLoveCountPO.getDynamicLoveCount()
                    ,CacheInstruction.DYNAMIC_EXPIRE_TIME
                            + random.nextInt((int) (CacheInstruction.DYNAMIC_EXPIRE_TIME * 0.05)));

            // 判断是否存在该动态的分数，若存在则加分，不存在则添加进去
            Double scoreInCache = redisCommonUtils.getScore(CacheInstruction.HOT_DYNAMIC_ID_SET
                    , dynamicLoveCountPO.getDynamicId().toString());
            if (scoreInCache == null){
                redisCommonUtils.zSet(CacheInstruction.HOT_DYNAMIC_ID_SET
                        ,dynamicLoveCountPO.getDynamicId().toString()
                        ,dynamicLoveCountPO.getDynamicLoveCount()*score
                        ,CacheInstruction.HOT_DYNAMIC_EXPIRE_TIME
                                + random.nextInt((int) (CacheInstruction.HOT_DYNAMIC_EXPIRE_TIME * 0.05)));
            }else {
                // 该动态有热度，增加热度值
                redisCommonUtils.incrementScoreForZSet(CacheInstruction.HOT_DYNAMIC_ID_SET
                        ,dynamicLoveCountPO.getDynamicId().toString()
                        ,score * dynamicLoveCountPO.getDynamicLoveCount()
                         + scoreInCache);
            }
        }
    }

    // 给动态加上对应的评论分数，并加入到热门动态中
//    @Test
    public void initialHotDynamicCommentScore() {
        Double score = dynamicHotOperateAddScoreConfig.getDynamicIncreaseScoreByOperateType(DynamicOperateType.DYNAMIC_COMMENT);
        List<DynamicCommentCountPO> dynamicCommentCountPOList = dynamicCommentMapper.countAllDynamic(DeleteStatus.NO);
        for (DynamicCommentCountPO dynamicCommentCountPO : dynamicCommentCountPOList) {
            // 初始化，动态评论数
            redisCommonUtils.hset(CacheInstruction.DYNAMIC_COMMENT_COUNT
                    , dynamicCommentCountPO.getDynamicId().toString(), dynamicCommentCountPO.getDynamicCommentCount()
                    , CacheInstruction.DYNAMIC_EXPIRE_TIME
                            + random.nextInt((int) (CacheInstruction.DYNAMIC_EXPIRE_TIME * 0.05)));

            // 判断是否存在该动态的分数，若存在则加分，不存在则添加进去
            Double scoreInCache = redisCommonUtils.getScore(CacheInstruction.HOT_DYNAMIC_ID_SET
                    , dynamicCommentCountPO.getDynamicId().toString());
            if (scoreInCache == null) {
                redisCommonUtils.zSet(CacheInstruction.HOT_DYNAMIC_ID_SET
                        , dynamicCommentCountPO.getDynamicId().toString(), dynamicCommentCountPO.getDynamicCommentCount() * score
                        , CacheInstruction.HOT_DYNAMIC_EXPIRE_TIME
                                + random.nextInt((int) (CacheInstruction.HOT_DYNAMIC_EXPIRE_TIME * 0.05)));
            } else {
                // 该动态有热度，增加热度值
                redisCommonUtils.incrementScoreForZSet(CacheInstruction.HOT_DYNAMIC_ID_SET
                        , dynamicCommentCountPO.getDynamicId().toString()
                        , score * dynamicCommentCountPO.getDynamicCommentCount()
                            + scoreInCache);
            }
        }
    }

    // 给动态加上对应的转发分数，并加入到热门动态中
//    @Test
    public void initialHotDynamicForwardScore() {
        Double score = dynamicHotOperateAddScoreConfig.getDynamicIncreaseScoreByOperateType(DynamicOperateType.DYNAMIC_FORWARD);
        List<DynamicForwardCountPO> dynamicForwardCountPOList = dynamicForwardMapper.countAllDynamic();
        for (DynamicForwardCountPO dynamicForwardCountPO : dynamicForwardCountPOList) {
            // 初始化，动态转发数
            redisCommonUtils.hset(CacheInstruction.DYNAMIC_FORWARD_COUNT
                    , dynamicForwardCountPO.getDynamicId().toString(), dynamicForwardCountPO.getDynamicForwardCount()
                    , CacheInstruction.DYNAMIC_EXPIRE_TIME
                            + random.nextInt((int) (CacheInstruction.DYNAMIC_EXPIRE_TIME * 0.05)));

            // 判断是否存在该动态的分数，若存在则加分，不存在则添加进去
            Double scoreInCache = redisCommonUtils.getScore(CacheInstruction.HOT_DYNAMIC_ID_SET
                    , dynamicForwardCountPO.getDynamicId().toString());
            if (scoreInCache == null) {
                redisCommonUtils.zSet(CacheInstruction.HOT_DYNAMIC_ID_SET
                        , dynamicForwardCountPO.getDynamicId().toString()
                        , dynamicForwardCountPO.getDynamicForwardCount() * score
                        , CacheInstruction.HOT_DYNAMIC_EXPIRE_TIME
                                + random.nextInt((int) (CacheInstruction.HOT_DYNAMIC_EXPIRE_TIME * 0.05)));

            } else {
                // 该动态有热度，增加热度值
                redisCommonUtils.incrementScoreForZSet(CacheInstruction.HOT_DYNAMIC_ID_SET
                        , dynamicForwardCountPO.getDynamicId().toString()
                        , score * dynamicForwardCountPO.getDynamicForwardCount()
                            + scoreInCache);
            }
        }
    }
}
